/**
 * Created by Administrator on 14-3-20.
 */

var acc=Physics.behavior('constant-acceleration',{ acc:{x:0,y:0.000}});
var mes=new unit();
Physics({
    timestep: 1000.0 / 160,
    // maximum number of iterations per step
    maxIPF: 16

},function(world){
    var viewWidth = map.width;
    var viewHeight = map.height;

    //console.log(world,world._integrator.options.drag);

    var renderer = Physics.renderer('canvas', {
        el: 'viewport',
        width: viewWidth,
        height: viewHeight,
        meta: false, // don't display meta data
        styles: {
            // set colors for the circle bodies
            'circle' : {
                strokeStyle: 'hsla(60, 37%, 17%, 1)',
                lineWidth: 2,
                fillStyle: 'hsla(60, 37%, 57%, 0.8)',
                angleIndicator: 'hsla(60, 37%, 17%, 0.4)'
            }

        }
    });

    // add the renderer
    world.add( renderer );
    // render on each step
    world.subscribe('step', function(){//循环步骤
        world.render();
    });
	
    world.subscribe('render', function( data ){

    });
    // bounds of the window
    var viewportBounds = Physics.aabb(0,0, viewWidth, viewHeight);
    // constrain objects to these bounds
    world.add(Physics.behavior('edge-collision-detection', {
        aabb: viewportBounds,
        restitution:map.edge_restitution,
        cof: 0.5
    }));

    // add a circle


    // ensure objects bounce when edge collision is detected
    world.add( Physics.behavior('body-impulse-response') );

    world.add(acc);
    // add some gravity
  /* world.add( Physics.behavior('constant-acceleration',{
        acc: { x : 0, y: 0.0001 }  }
    ) );*/
    // subscribe to ticker to advance the simulation
    var square = Physics.body('convex-polygon', {
        x: 250,
        y: 250,
        mass:1,
        vertices: [
            {x: 0, y: 50},
            {x: 50, y: 50},
            {x: 50, y: 0},
            {x: 0, y: 0}
        ]
    });
   // world.add( square );
   // console.log(map.player);

    function init(){
if(map.player)
 world.add(map.player);
 if(map.enemy)
  world.add(map.enemy);
  if(map.npc)
   world.add(map.npc);
    world._integrator.options.drag=map.darg;
        mes.bind('player',map.player);
        mes.bind('enemy',map.enemy);
        mes.bind('npc',map.npc);
    }
    init();
    world.subscribe('collisions:detected',function(data){
      //  console.log('col')
    });
    world.add( Physics.behavior('body-collision-detection') );
    world.add( Physics.behavior('sweep-prune'));
    world.add( Physics.behavior('player-behavior',{player: map.player}));
    world.add( Physics.behavior('body-impulse-response'  ));


    Physics.util.ticker.subscribe(function( time, dt ){
        if(map.enemy){
            if(map.enemy.run)
            map.enemy.run();
        }
        if(map.npc){
            if(map.npc.run)
                map.npc.run();
        }
        world.step( time );
    });

    // start the ticker
    Physics.util.ticker.start();

});
